#include "classdiagrammdock.h"

ClassDiagrammDock::ClassDiagrammDock(QWidget *parent) : QDockWidget(parent)
{
    this->setWindowTitle(tr("Classdiagramm Toolbox"));
    this->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

    this->_layout = new QBoxLayout(QBoxLayout::TopToBottom, &this->_widget);
    this->_widget.setLayout(this->_layout);
    this->initItemBar();
    this->setWidget(&this->_widget);
}

ClassDiagrammDock::~ClassDiagrammDock()
{
    qDebug("deleting classDiagrammDock");

    while (!this->_buttons.isEmpty()) delete this->_buttons.takeFirst();

    delete this->_layout;
}

void ClassDiagrammDock::initItemBar()
{
    QPushButton *classItem = new QPushButton(this);
    classItem->setIcon(QIcon(":/icons/packagebrowser/class.png"));
    classItem->setIconSize(QSize(40, 40));
    classItem->setStyleSheet(this->buttonStyle());
    classItem->setText(tr("Class"));
    QObject::connect(classItem, SIGNAL(pressed()), this, SLOT(createNewClass()));
    this->_layout->addWidget(classItem);
    this->_buttons.append(classItem);

    QPushButton *absClassItem = new QPushButton(this);
    absClassItem->setIcon(QIcon(":/icons/packagebrowser/absclass.png"));
    absClassItem->setIconSize(QSize(40, 40));
    absClassItem->setStyleSheet(this->buttonStyle());
    absClassItem->setText(tr("Abstract Class"));
    QObject::connect(absClassItem, SIGNAL(pressed()), this, SLOT(createNewAbstractClass()));
    this->_layout->addWidget(absClassItem);
    this->_buttons.append(absClassItem);

    QPushButton *interfaceItem = new QPushButton(this);
    interfaceItem->setIcon(QIcon(":/icons/packagebrowser/interface.png"));
    interfaceItem->setIconSize(QSize(40, 40));
    interfaceItem->setStyleSheet(this->buttonStyle());
    interfaceItem->setText(tr("Interface"));
    QObject::connect(interfaceItem, SIGNAL(pressed()), this, SLOT(createNewInterface()));
    this->_layout->addWidget(interfaceItem);
    this->_buttons.append(interfaceItem);

    QPushButton *packageItem = new QPushButton(this);
    packageItem->setIcon(QIcon(":/icons/packagebrowser/package.png"));
    packageItem->setIconSize(QSize(40, 40));
    packageItem->setStyleSheet(this->buttonStyle());
    packageItem->setText(tr("Package"));
    QObject::connect(packageItem, SIGNAL(pressed()), this, SLOT(createNewPackage()));
    this->_layout->addWidget(packageItem);
    this->_buttons.append(packageItem);
}

QString ClassDiagrammDock::buttonStyle()
{
    QString style = "QPushButton {text-align:left; border: none; height: 40px; padding: 3px; subcontrol-position: left;} ";
    style += "QPushButton:hover {border: 1px solid #f90; background: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffc, stop: 1 #fc6);}";

    return style;
}

void ClassDiagrammDock::createNewClass()
{
    QMimeData *mimeData = new QMimeData();
    mimeData->setText("add_classitem");

    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->setPixmap(QPixmap(":/icons/packagebrowser/class.png"));
    drag->exec();
}

void ClassDiagrammDock::createNewAbstractClass()
{
    QMimeData *mimeData = new QMimeData();
    mimeData->setText("add_abstract_classitem");

    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->setPixmap(QPixmap(":/icons/packagebrowser/absclass.png"));
    drag->exec();
}

void ClassDiagrammDock::createNewInterface()
{
    QMimeData *mimeData = new QMimeData();
    mimeData->setText("add_interface");

    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->setPixmap(QPixmap(":/icons/packagebrowser/interface.png"));
    drag->exec();
}

void ClassDiagrammDock::createNewPackage()
{
    QMimeData *mimeData = new QMimeData();
    mimeData->setText("add_package");

    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->setPixmap(QPixmap(":/icons/packagebrowser/package.png"));
    drag->exec();
}
